home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / NewsView 1.0.0 / source / FindDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-30  |  12.6 KB  |  415 lines  |  [TEXT/KAHL]

  1. /* FindDialog.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Offline USENET News Viewer                                             */
  5. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  6. /*                                                                           */
  7. /*    This software is Public Domain; it may be used for any purpose         */
  8. /*    whatsoever without restriction.                                        */
  9. /*                                                                           */
  10. /*    This package is distributed in the hope that it will be useful,        */
  11. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  12. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  13. /*                                                                           */
  14. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  15. /*                                                                           */
  16. /*****************************************************************************/
  17.  
  18. #include "MiscInfo.h"
  19. #include "Audit.h"
  20. #include "Debug.h"
  21. #include "Definitions.h"
  22.  
  23. #include "FindDialog.h"
  24. #include "Screen.h"
  25. #include "EventLoop.h"
  26. #include "SimpleButton.h"
  27. #include "TextEdit.h"
  28. #include "Memory.h"
  29. #include "Alert.h"
  30. #include "Menus.h"
  31.  
  32.  
  33. #define WIDTH (490)
  34. #define HEIGHT (114)
  35.  
  36. #define SEARCHPROMPTX (13)
  37. #define SEARCHPROMPTY (11)
  38.  
  39. #define SEARCHEDITX (114)
  40. #define SEARCHEDITY (8)
  41. #define SEARCHEDITWIDTH (364)
  42. #define SEARCHEDITHEIGHT (22)
  43.  
  44. #define REPLACEPROMPTX (13)
  45. #define REPLACEPROMPTY (37)
  46.  
  47. #define REPLACEEDITX (114)
  48. #define REPLACEEDITY (34)
  49. #define REPLACEEDITWIDTH (364)
  50. #define REPLACEEDITHEIGHT (22)
  51.  
  52. #define FROMSTARTX (6)
  53. #define FROMSTARTY (74)
  54. #define FROMSTARTWIDTH (111)
  55. #define FROMSTARTHEIGHT (21)
  56.  
  57. #define FINDAGAINX (128)
  58. #define FINDAGAINY (74)
  59. #define FINDAGAINWIDTH (111)
  60. #define FINDAGAINHEIGHT (21)
  61.  
  62. #define DONTFINDX (249)
  63. #define DONTFINDY (74)
  64. #define DONTFINDWIDTH (111)
  65. #define DONTFINDHEIGHT (21)
  66.  
  67. #define CANCELX (369)
  68. #define CANCELY (74)
  69. #define CANCELWIDTH (111)
  70. #define CANCELHEIGHT (21)
  71.  
  72.  
  73. typedef struct
  74.     {
  75.         WinType*                            ScreenID;
  76.         TextEditRec*                    StringToFind;
  77.         TextEditRec*                    StringToReplace;
  78.         TextEditRec*                    ActiveTextEdit;
  79.         SimpleButtonRec*            FindFromStartButton;
  80.         SimpleButtonRec*            FindAgainButton;
  81.         SimpleButtonRec*            DontFindButton;
  82.         SimpleButtonRec*            CancelButton;
  83.     } FindDialogRec;
  84.  
  85.  
  86. static void        LocalUpdate(FindDialogRec* Window)
  87.     {
  88.         CheckPtrExistence(Window);
  89.         SetClipRect(Window->ScreenID,0,0,WIDTH,HEIGHT);
  90.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Search for:",11,
  91.             SEARCHPROMPTX,SEARCHPROMPTY,ePlain);
  92.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Replace with:",13,
  93.             REPLACEPROMPTX,REPLACEPROMPTY,ePlain);
  94.         RedrawSimpleButton(Window->FindFromStartButton);
  95.         RedrawSimpleButton(Window->FindAgainButton);
  96.         RedrawSimpleButton(Window->DontFindButton);
  97.         RedrawSimpleButton(Window->CancelButton);
  98.         TextEditFullRedraw(Window->StringToFind);
  99.         TextEditFullRedraw(Window->StringToReplace);
  100.     }
  101.  
  102.  
  103. FindOpType        DoFindDialog(char** SearchKey, char** Replace,
  104.                                 MenuItemType* MCut, MenuItemType* MPaste, MenuItemType* MCopy,
  105.                                 MenuItemType* MUndo, MenuItemType* MSelectAll, MenuItemType* MClear)
  106.     {
  107.         FindOpType                    ReturnCode;
  108.         FindDialogRec*            Window;
  109.  
  110.         /* make sure the things exist */
  111.         CheckPtrExistence(*SearchKey);
  112.         CheckPtrExistence(*Replace);
  113.  
  114.         Window = (FindDialogRec*)AllocPtrCanFail(sizeof(FindDialogRec),"FindDialogRec");
  115.         if (Window == NIL)
  116.             {
  117.              FailurePoint1:
  118.                 AlertHalt("There is not enough memory to display the dialog box.",NIL);
  119.                 return eFindCancel;
  120.             }
  121.  
  122.         Window->ScreenID = MakeNewWindow(eDialogWindow,eWindowNotClosable,eWindowNotZoomable,
  123.             eWindowNotResizable,DialogLeftEdge(WIDTH),DialogTopEdge(HEIGHT),WIDTH,HEIGHT,
  124.             (void (*)(void*))&LocalUpdate,Window);
  125.         if (Window->ScreenID == 0)
  126.             {
  127.              FailurePoint2:
  128.                 ReleasePtr((char*)Window);
  129.                 goto FailurePoint1;
  130.             }
  131.  
  132.         Window->StringToFind = NewTextEdit(Window->ScreenID,eTENoScrollBars,GetScreenFont(),
  133.             9,SEARCHEDITX,SEARCHEDITY,SEARCHEDITWIDTH,SEARCHEDITHEIGHT);
  134.         if (Window->StringToFind == NIL)
  135.             {
  136.              FailurePoint3:
  137.                 KillWindow(Window->ScreenID);
  138.                 goto FailurePoint2;
  139.             }
  140.         TextEditNewRawData(Window->StringToFind,*SearchKey,SYSTEMLINEFEED);
  141.         TextEditDoMenuSelectAll(Window->StringToFind);
  142.         Window->ActiveTextEdit = Window->StringToFind;
  143.         EnableTextEditSelection(Window->ActiveTextEdit);
  144.  
  145.         Window->StringToReplace = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  146.             GetScreenFont(),9,REPLACEEDITX,REPLACEEDITY,REPLACEEDITWIDTH,REPLACEEDITHEIGHT);
  147.         if (Window->StringToReplace == NIL)
  148.             {
  149.              FailurePoint4:
  150.                 DisposeTextEdit(Window->StringToFind);
  151.                 goto FailurePoint3;
  152.             }
  153.         TextEditNewRawData(Window->StringToReplace,*Replace,SYSTEMLINEFEED);
  154.         TextEditDoMenuSelectAll(Window->StringToReplace);
  155.  
  156.         Window->FindFromStartButton = NewSimpleButton(Window->ScreenID,"Find From Start",
  157.             FROMSTARTX,FROMSTARTY,FROMSTARTWIDTH,FROMSTARTHEIGHT);
  158.         if (Window->FindFromStartButton == NIL)
  159.             {
  160.              FailurePoint5:
  161.                 DisposeTextEdit(Window->StringToReplace);
  162.                 goto FailurePoint4;
  163.             }
  164.  
  165.         Window->FindAgainButton = NewSimpleButton(Window->ScreenID,"Find Again",
  166.             FINDAGAINX,FINDAGAINY,FINDAGAINWIDTH,FINDAGAINHEIGHT);
  167.         if (Window->FindAgainButton == NIL)
  168.             {
  169.              FailurePoint6:
  170.                 DisposeSimpleButton(Window->FindFromStartButton);
  171.                 goto FailurePoint5;
  172.             }
  173.         SetDefaultButtonState(Window->FindAgainButton,True);
  174.  
  175.         Window->DontFindButton = NewSimpleButton(Window->ScreenID,"Don't Find",
  176.             DONTFINDX,DONTFINDY,DONTFINDWIDTH,DONTFINDHEIGHT);
  177.         if (Window->DontFindButton == NIL)
  178.             {
  179.              FailurePoint7:
  180.                 DisposeSimpleButton(Window->FindAgainButton);
  181.                 goto FailurePoint6;
  182.             }
  183.  
  184.         Window->CancelButton = NewSimpleButton(Window->ScreenID,"Cancel",
  185.             CANCELX,CANCELY,CANCELWIDTH,CANCELHEIGHT);
  186.         if (Window->CancelButton == NIL)
  187.             {
  188.              FailurePoint8:
  189.                 DisposeSimpleButton(Window->DontFindButton);
  190.                 goto FailurePoint7;
  191.             }
  192.  
  193.         /* now do our loco event loop */
  194.         while (1)
  195.             {
  196.                 OrdType                            X;
  197.                 OrdType                            Y;
  198.                 ModifierFlags                Modifiers;
  199.                 MenuItemType*                MenuItem;
  200.                 char                                KeyPress;
  201.  
  202.                 switch (GetAnEvent(&X,&Y,&Modifiers,NIL,&MenuItem,&KeyPress))
  203.                     {
  204.                         default:
  205.                             break;
  206.                         case eCheckCursor:
  207.                             if (TextEditIBeamTest(Window->StringToFind,X,Y)
  208.                                 || (TextEditIBeamTest(Window->StringToReplace,X,Y)))
  209.                                 {
  210.                                     SetIBeamCursor();
  211.                                 }
  212.                              else
  213.                                 {
  214.                                     SetArrowCursor();
  215.                                 }
  216.                             goto NoEventPoint;
  217.                             break;
  218.                         case eNoEvent:
  219.                          NoEventPoint:
  220.                             TextEditUpdateCursor(Window->ActiveTextEdit);
  221.                             break;
  222.                         case eMenuStarting:
  223.                             EnableMenuItem(MPaste);
  224.                             if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  225.                                 {
  226.                                     EnableMenuItem(MCut);
  227.                                     EnableMenuItem(MCopy);
  228.                                     EnableMenuItem(MClear);
  229.                                 }
  230.                             EnableMenuItem(MSelectAll);
  231.                             if (TextEditCanWeUndo(Window->ActiveTextEdit))
  232.                                 {
  233.                                     EnableMenuItem(MUndo);
  234.                                 }
  235.                             break;
  236.                         case eMenuCommand:
  237.                             if (MenuItem == MPaste)
  238.                                 {
  239.                                     TextEditDoMenuPaste(Window->ActiveTextEdit);
  240.                                 }
  241.                             else if (MenuItem == MCut)
  242.                                 {
  243.                                     TextEditDoMenuCut(Window->ActiveTextEdit);
  244.                                 }
  245.                             else if (MenuItem == MCopy)
  246.                                 {
  247.                                     TextEditDoMenuCopy(Window->ActiveTextEdit);
  248.                                 }
  249.                             else if (MenuItem == MClear)
  250.                                 {
  251.                                     TextEditDoMenuClear(Window->ActiveTextEdit);
  252.                                 }
  253.                             else if (MenuItem == MUndo)
  254.                                 {
  255.                                     TextEditDoMenuUndo(Window->ActiveTextEdit);
  256.                                     TextEditShowSelection(Window->ActiveTextEdit);
  257.                                 }
  258.                             else if (MenuItem == MSelectAll)
  259.                                 {
  260.                                     TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  261.                                 }
  262.                             else
  263.                                 {
  264.                                     EXECUTE(PRERR(AllowResume,
  265.                                         "DoFindDialog: Undefined menu option chosen"));
  266.                                 }
  267.                             break;
  268.                         case eKeyPressed:
  269.                             if (KeyPress == 13)
  270.                                 {
  271.                                     if ((Modifiers & eCommandKey) != 0)
  272.                                         {
  273.                                             /* command-return means actually interpret the return key */
  274.                                             /* in this implementation, multi-line search keys aren't */
  275.                                             /* supported, but in case we get around to changing it, */
  276.                                             /* we have this bit of code here */
  277.                                             /* TextEditDoKeyPressed(Window->ActiveTextEdit,KeyPress, */
  278.                                             /* Modifiers & ~eCommandKey); */
  279.                                         }
  280.                                      else
  281.                                         {
  282.                                             /* otherwise, leave the box & do the thing. */
  283.                                             FlashButton(Window->FindAgainButton);
  284.                                             ReturnCode = eFindAgain;
  285.                                             goto AllDonePoint;
  286.                                         }
  287.                                 }
  288.                             else if (KeyPress == 9)
  289.                                 {
  290.                                     if ((Modifiers & eCommandKey) != 0)
  291.                                         {
  292.                                             /* command-tab means actually interpret the return key */
  293.                                             TextEditDoKeyPressed(Window->ActiveTextEdit,KeyPress,
  294.                                                 Modifiers & ~eCommandKey);
  295.                                         }
  296.                                      else
  297.                                         {
  298.                                             /* otherwise, switch to the other text entry box */
  299.                                             DisableTextEditSelection(Window->ActiveTextEdit);
  300.                                             if (Window->ActiveTextEdit == Window->StringToFind)
  301.                                                 {
  302.                                                     Window->ActiveTextEdit = Window->StringToReplace;
  303.                                                 }
  304.                                              else
  305.                                                 {
  306.                                                     Window->ActiveTextEdit = Window->StringToFind;
  307.                                                 }
  308.                                             TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  309.                                             EnableTextEditSelection(Window->ActiveTextEdit);
  310.                                         }
  311.                                 }
  312.                             else if (KeyPress == eCancelKey)
  313.                                 {
  314.                                     FlashButton(Window->CancelButton);
  315.                                     ReturnCode = eFindCancel;
  316.                                     goto AllDonePoint;
  317.                                 }
  318.                             else
  319.                                 {
  320.                                     TextEditDoKeyPressed(Window->ActiveTextEdit,KeyPress,Modifiers);
  321.                                 }
  322.                             break;
  323.                         case eMouseDown:
  324.                             if (SimpleButtonHitTest(Window->FindFromStartButton,X,Y))
  325.                                 {
  326.                                     if (SimpleButtonMouseDown(Window->FindFromStartButton,X,Y,NIL,NIL))
  327.                                         {
  328.                                             ReturnCode = eFindFromStart;
  329.                                             goto AllDonePoint;
  330.                                         }
  331.                                 }
  332.                             else if (SimpleButtonHitTest(Window->FindAgainButton,X,Y))
  333.                                 {
  334.                                     if (SimpleButtonMouseDown(Window->FindAgainButton,X,Y,NIL,NIL))
  335.                                         {
  336.                                             ReturnCode = eFindAgain;
  337.                                             goto AllDonePoint;
  338.                                         }
  339.                                 }
  340.                             else if (SimpleButtonHitTest(Window->DontFindButton,X,Y))
  341.                                 {
  342.                                     if (SimpleButtonMouseDown(Window->DontFindButton,X,Y,NIL,NIL))
  343.                                         {
  344.                                             ReturnCode = eDontFind;
  345.                                             goto AllDonePoint;
  346.                                         }
  347.                                 }
  348.                             else if (SimpleButtonHitTest(Window->CancelButton,X,Y))
  349.                                 {
  350.                                     if (SimpleButtonMouseDown(Window->CancelButton,X,Y,NIL,NIL))
  351.                                         {
  352.                                             ReturnCode = eFindCancel;
  353.                                             goto AllDonePoint;
  354.                                         }
  355.                                 }
  356.                             else if (TextEditHitTest(Window->StringToFind,X,Y))
  357.                                 {
  358.                                     if (Window->ActiveTextEdit != Window->StringToFind)
  359.                                         {
  360.                                             DisableTextEditSelection(Window->ActiveTextEdit);
  361.                                             Window->ActiveTextEdit = Window->StringToFind;
  362.                                             EnableTextEditSelection(Window->ActiveTextEdit);
  363.                                         }
  364.                                     TextEditDoMouseDown(Window->ActiveTextEdit,X,Y,Modifiers);
  365.                                 }
  366.                             else if (TextEditHitTest(Window->StringToReplace,X,Y))
  367.                                 {
  368.                                     if (Window->ActiveTextEdit == Window->StringToReplace)
  369.                                         {
  370.                                             DisableTextEditSelection(Window->ActiveTextEdit);
  371.                                             Window->ActiveTextEdit = Window->StringToReplace;
  372.                                             EnableTextEditSelection(Window->ActiveTextEdit);
  373.                                         }
  374.                                     TextEditDoMouseDown(Window->ActiveTextEdit,X,Y,Modifiers);
  375.                                 }
  376.                             break;
  377.                     }
  378.             }
  379.      AllDonePoint:
  380.         if (ReturnCode != eFindCancel)
  381.             {
  382.                 char*                NewFindStuff;
  383.                 char*                NewReplaceStuff;
  384.  
  385.                 NewFindStuff = TextEditGetRawData(Window->StringToFind,SYSTEMLINEFEED);
  386.                 if (NewFindStuff == NIL)
  387.                     {
  388.                      EndFailure1:
  389.                         ReturnCode = eFindCancel;
  390.                         goto ReleaseAndExit;
  391.                     }
  392.                 NewReplaceStuff = TextEditGetRawData(Window->StringToReplace,SYSTEMLINEFEED);
  393.                 if (NewReplaceStuff == NIL)
  394.                     {
  395.                      EndFailure2:
  396.                         ReleasePtr(NewFindStuff);
  397.                         goto EndFailure1;
  398.                     }
  399.                 ReleasePtr(*SearchKey);
  400.                 *SearchKey = NewFindStuff;
  401.                 ReleasePtr(*Replace);
  402.                 *Replace = NewReplaceStuff;
  403.             }
  404.      ReleaseAndExit:
  405.         DisposeTextEdit(Window->StringToFind);
  406.         DisposeTextEdit(Window->StringToReplace);
  407.         DisposeSimpleButton(Window->FindFromStartButton);
  408.         DisposeSimpleButton(Window->FindAgainButton);
  409.         DisposeSimpleButton(Window->DontFindButton);
  410.         DisposeSimpleButton(Window->CancelButton);
  411.         KillWindow(Window->ScreenID);
  412.         ReleasePtr((char*)Window);
  413.         return ReturnCode;
  414.     }
  415.